import {request} from "../../request/index.js"
// pages/goods_detail/goods_detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObj:{},
    isCollect:true
  },
  //商品对象
  GoodsInfo:{},
  handlePreviewImage(e){
    const index=e.currentTarget.dataset.index
    const urls=this.GoodsInfo.pics.map(v=>v.pics_mid)
    wx.previewImage({
      current:urls[index],
      urls:urls
    })
  },
  handleCollect(){
    if (!wx.getStorageSync("userInfo")){
      wx.navigateTo({
        url:'/pages/login/login'
      })
      return;
    }
    let isCollect=false;
    let collect=wx.getStorageSync("collect")||[];
    let index=collect.findIndex(v=>v.goods_id===this.GoodsInfo.goods_id)
    if (index!==-1){
      collect.splice(index,1);
      isCollect=false;
      wx.showToast({
        title:"取消成功",
        mask:true
      })
    }else {
      collect.push(this.GoodsInfo)
      isCollect=true
      wx.showToast({
        title:"收藏成功",
        mask:true
      })
    }
    wx.setStorageSync("collect",collect)
    this.setData({
      isCollect
    })

  },
  handleCartAdd(){
    let cart = wx.getStorageSync("cart")||[]
    let index=cart.findIndex(v=>v.goods_id===this.GoodsInfo.goods_id)
    if (index===-1){
      this.GoodsInfo.num=1;
      this.GoodsInfo.checked=true;
      cart.push(this.GoodsInfo)
    }else {
      cart[index].num++;
    }
    wx.setStorageSync("cart",cart);
    wx.showToast({
      title:"加入成功",
      icon:'success',
      mask:true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    let pages=getCurrentPages()
    let currentPage=pages[pages.length-1]
    let options=currentPage.options
    const goods_id=options.goods_id
    this.getGoodsDetail(goods_id)

  },
  getGoodsDetail(goods_id){
    request({url:"/goods/detail",data: {goods_id}}).then(res=>{
      this.GoodsInfo=res.data.message
      let collect=wx.getStorageSync("collect")||[];
      let isCollect=collect.some(v=>v.goods_id===this.GoodsInfo.goods_id)
      this.setData({
        goodsObj:{
          goods_name:res.data.message.goods_name,
          goods_price:res.data.message.goods_price,
          //iphone部分手机不识别webp图片格式
          //最好找到后台工程师让她进行修改
          goods_introduce:res.data.message.goods_introduce.replace(/\.webp/g,'.jpg'),
          pics:res.data.message.pics
        },
        isCollect
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})